package com.bw.yk08;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import org.apache.flink.client.program.StreamContextEnvironment;
import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Test01 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env=StreamExecutionEnvironment.createLocalEnvironment();
        env.setParallelism(1);

        env.enableCheckpointing(5000L);
        StateBackend stateBackend=new FsStateBackend("files://my/checkpoint/dir");
        env.setStateBackend(stateBackend);
//        env.getCheckpointConfig().setCheckpointStorage("files:///my/checkpoint/dir");
        env.getCheckpointConfig().enableExternalizedCheckpoints(
                CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        // -s C:\data\flink\checkpoints\consumer1\cfaa0b0fe2ebcb597c706be2c9bb6913\chk-9

        DebeziumSourceFunction<String> mysqlSource=MySQLSource.<String>builder()
                .hostname("hadoop-single")
                .port(3306)
                .username("root")
                .password("root")
                .databaseList("fastfood")
                .startupOptions(StartupOptions.initial())  //全量
//                .startupOptions(StartupOptions.latest())   //增量
                .deserializer(new MyDebeziumDeserializationSchema())
                .build();

        DataStreamSource<String> mysqlDS=env.addSource(mysqlSource);

        mysqlDS.print();

        //写入kafka

        mysqlDS.addSink(MyKafkaUtil.getFlinkKafkaProducer("ods_base_topic"));

        env.execute();
    }
}
